package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Employee;
import cn.itsource.system.domain.Tenant;
import cn.itsource.system.dto.EnteringDTO;
import cn.itsource.system.mapper.EmployeeMapper;
import cn.itsource.system.mapper.MealMapper;
import cn.itsource.system.mapper.TenantMapper;
import cn.itsource.system.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kxy
 * @since 2020-02-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private MealMapper mealMapper;

    @Override
    public void entering(EnteringDTO enteringDTO) {
        //1.先保存租户
        Tenant tenant = enteringDTO.getTenant();
        Date date = new Date();
        tenant.setRegisterTime(date);
        baseMapper.insert(tenant);
        //2.在保存租户的管理员账号
        Employee employee = enteringDTO.getEmployee();
        //保存时间
        employee.setInputTime(date);
        //员工的状态 正常
        employee.setState(Employee.STATE_NORMAL);
        //机构id
        employee.setTenantId(tenant.getId());
        //用户类型 管理员
        employee.setType(Employee.TYPE_ADMIN);
        employeeMapper.insert(employee);
        //3.租户和套餐的中间表
        mealMapper.insertRelationMealAndTenant(enteringDTO.getMealId(),tenant.getId(),0);
    }
}
